package com.chat.io;


import java.io.*;

/*
  文件转换
 */
public class ChangeImage {
       //文件转换成二进制
     public byte[] get(File file){
         System.out.println(file);
         try {
             FileInputStream fis=new FileInputStream(file);
             ByteArrayOutputStream bos=new ByteArrayOutputStream(1024);
             byte[] b=new byte[1024];
             int n;
             while((n=fis.read(b))!=-1){
                   bos.write(b,0,n);
             }

             byte[] data=bos.toByteArray();
             System.out.println("获取成功");
             fis.close();
             bos.close();
             return data;
         } catch (IOException e) {
             e.printStackTrace();
         }
            return null;
     }
     //二进制转换成文件
    public void set(String username,byte[] data){
        try {
            FileOutputStream fos=new FileOutputStream(new File("D:/chatproject/client/"+username+".png"));
            fos.write(data,0,data.length);
            System.out.println("加载图片");
            System.out.println(fos.toString());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
